<?php

namespace app\api\controller\ai;

use app\common\controller\BaseController;
use app\common\service\ai\BillService;
use app\common\service\ai\WxPayService;
use app\common\service\member\MemberService;
use hg\apidoc\annotation as Apidoc;
use app\common\service\ai\SystemConfigService;

/**
 * @Apidoc\Title("充值中心管理")
 * @Apidoc\Group("ai")
 * @Apidoc\Sort("300")
 */
class Recharge extends BaseController {

    /**
     * @Apidoc\Title("列表")
     * @Apidoc\Returned("list", type="array", desc="列表", children={
     * @Apidoc\Returned(ref="app\common\model\ai\OrderModel", field="id,sale_price,point"),
     * })
     */
    public function getList() {
        list($recharge_amount, $point_exchange, $list) = ['', 0, []];
        // 配置
        $config_list = SystemConfigService::getList('RECHARGE_AMOUNT,POINT_EXCHANGE', 'name,value');
        foreach ($config_list as $key => $config) {
            if ($config['name'] == 'RECHARGE_AMOUNT') {
                $recharge_amount = $config['value'];
            }
            if ($config['name'] == 'POINT_EXCHANGE') {
                $point_exchange = $config['value'];
            }
        }
        // 充值金额点数
        foreach (explode(',', $recharge_amount) as $key => $value) {
            $list[$key]['sale_price'] = $value;
            $list[$key]['point'] = $value * $point_exchange;
        }
        return success($list);
    }


    /**
     * @Apidoc\Title("支付")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\ai\BillModel", field="order_no")
     */
    public function pay() {

        $data = BillService::createBill($this->data);
        return success($data);
    }


    /**
     * @Apidoc\Title("回调")
     * @Apidoc\Method("POST")
     */
    public function notify() {
        $wxPayService = new \app\common\service\ai\WxPayService();
        $xml = file_get_contents('php://input');
        return $wxPayService->handleWeChatPayCallback($xml);
    }

}
